﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI
{
    public partial class InvoiceAmendment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillRemitter();
            }
        }

        private void FillRemitter()
        {
            //clear existing list
            ddlRemitter.Items.Clear();

            RemitterEntity lstEntity = new RemitterEntity();
            List<Remitter> remitterList = lstEntity.GetRemitterIdForMatchingCriteria(((User)Session["CurrentUserVO"]).CustomerID);


            if (remitterList != null && remitterList.Count<Remitter>() > 0)
            {
                ddlRemitter.Items.Add(new ListItem("<Select>", "0"));
                foreach (Remitter remitter in remitterList)
                {
                    ddlRemitter.Items.Add(new ListItem(remitter.RemitterName, remitter.RemitterID));
                    //listRemitterId.Append("'"+ remitter.RemitterID +"',");
                }

                ddlRemitter.SelectedIndex = 0;
            }
        }
        protected void ddlRemitter_SelectedIndexChanged(object sender, EventArgs e)
        {

            PopulateData();
        }

        private void PopulateData()
        {
            Lists lstEntity = new Lists();
            List<Invoice> dtInvoice = lstEntity.GetInvoicesForAmendment(((User)Session["CurrentUserVO"]).CustomerID, ddlRemitter.SelectedValue.Trim());
            Session["UnMatchedInvoiceList"] = dtInvoice;
            grdInvoice.DataSource = dtInvoice;
            grdInvoice.DataBind();
        }


        protected void EditRow(object sender, GridViewEditEventArgs e)
        {

            grdInvoice.EditIndex = e.NewEditIndex;

            this.PopulateData();

        }



        protected void CancelEditRow(object sender, GridViewCancelEditEventArgs e)
        {

            grdInvoice.EditIndex = -1;

            //  this.PopulateData();

        }



        protected void DeleteRow(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = (GridViewRow)grdInvoice.Rows[e.RowIndex];
            string ID = row.Cells[1].Text;
            grdInvoice.DeleteRow(e.RowIndex);

            // this.PopulateData();



        }



        protected void ChangePage(object sender, GridViewPageEventArgs e)
        {

            grdInvoice.PageIndex = e.NewPageIndex;

            //  this.PopulateData();

        }

        protected void lnkDelete_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            GridViewRow row = (GridViewRow)img.Parent.Parent;

            Lists listIn = new Lists();
            int result = listIn.DeleteInvoice(row.Cells[1].Text);
            this.PopulateData();


        }

        protected void lnkEdit_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            GridViewRow row = (GridViewRow)img.Parent.Parent;
        }

        protected void btnDeleteAll_Click(object sender, EventArgs e)
        {
            List<Invoice> dtRemitterList = new List<Invoice>();

            int invoiceRowNumber = 0, invoiceAdded = 0;

            foreach (GridViewRow invoiceRow in grdInvoice.Rows)
            {
                invoiceRowNumber++;
                System.Web.UI.HtmlControls.HtmlInputCheckBox chkSelect = ((System.Web.UI.HtmlControls.HtmlInputCheckBox)invoiceRow.Cells[0].FindControl("chkSelect"));
                if (chkSelect.Checked)
                {
                    invoiceAdded++;

                    Literal ltlInvoiceUniqueID = ((Literal)invoiceRow.Cells[0].FindControl("ltlInvoiceUniqueID"));

                    /*    DateTime dt; //date time variable for correct invoice date parsing
                        Invoice invoice = new Invoice
                        {
                            InvoiceID = ltlInvoiceUniqueID.Text,
                            InvoiceNumber = invoiceRow.Cells[1].Text,
                        
                        };*/
                    Lists obj = new Lists();
                    obj.DeleteInvoice(invoiceRow.Cells[1].Text);
                }

            }
            this.PopulateData();

        }

        protected void grdInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                double value = 0.00;
                if (e.Row.Cells[4].Text != "" && e.Row.Cells[4].Text != "&nbsp;")
                {
                    value = Convert.ToDouble(e.Row.Cells[4].Text);
                    if (value < 0)
                    {
                        e.Row.BackColor = System.Drawing.Color.Cyan;
                        value *= -1;
                    }
                    e.Row.Cells[4].Text = value.ToString("#,#.00", System.Globalization.CultureInfo.InvariantCulture);
                    e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
                }
                else
                {
                    e.Row.Cells[4].Text = "0.00";
                }
               
                DateTime dt;
                if (e.Row.Cells[2].Text != "" && e.Row.Cells[2].Text != "&nbsp;")
                {
                    dt = Convert.ToDateTime(e.Row.Cells[2].Text);
                    e.Row.Cells[2].Text = dt.ToString("dd-MM-yyyy");
                    e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
                }
                else
                {
                    e.Row.Cells[2].Text = "";
                }
            }
        }

    }
}